#include "stdafx.h"
#include <queue>

#include <stack>

TEST(shadow, v)
{
	fstream file;
	char sysInputFile[] = "jack.txt";
	file.open(sysInputFile);
	string lineBuffer;
	size_t tcount = -1;
	int n = -1, m = -1;
	queue<string> ops;
	stack<int> his;
	his.push(1);

	while (!file.eof())
	{
		getline(file, lineBuffer);
		if (lineBuffer.length() == 0)
			continue; //ignore all empty lines
		else 
		{
			
			//do something here
			if (-1 == tcount)
			{
				tcount = atoi(lineBuffer.c_str());
				continue;
			}
			if (tcount <= 0)
			{
				break;
			}
			
			if (-1 == n || -1 == m)
			{
				n = atoi(lineBuffer.c_str());
				if (n <= 0)
				{
					cout<<0<<endl;
					break;
				}
				lineBuffer.erase(0, lineBuffer.find(" ", 1));
				if (lineBuffer.empty())
				{
					cout<<1<<endl;
					break;
				}
				m = atoi(lineBuffer.c_str());
				if (m <= 0)
				{
					cout<<1<<endl;
					break;
				}
				continue;
			}

			ops.push(lineBuffer);
			if (ops.size() == m)
			{
				while (!ops.empty())
				{
					
					string front = ops.front();
					
					cout<<front<<endl;
					if (0 == front.compare("PRE"))
					{
						his.pop();
						if (his.empty())
						{
							his.push(1);
						} else
						{
							his.push(his.top());
						}
						cout<<his.top()<<endl;
					} else if (0 == front.compare("NEXT"))
					{
						if (his.top() != n)
						{
							his.push(his.top() + 1);
						}
						cout<<his.top()<<endl;
					} else
					{
						front.erase(0, front.find(" ", 1));
						if (front.empty())
						{
							cerr<<"err in get PLAY:"<<endl;
						}
						int play_num = atoi(front.c_str());
						if (0 >= play_num)
						{
							cerr<<"err in get PLAY NUM:"<<endl;
						}
						if (his.empty() || his.top() != play_num)
						{
							his.push(play_num);
						}
						cout<<play_num<<endl;
					}
					ops.pop();
				}
				--tcount;
			}

		}
	}


}